package com.ruoyi.easyExcelHandler.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.util.HashMap;

public class ProductStatusConverter implements Converter<String> {

    private String val;

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 这里读的时候会调用
     *
     * @param context
     * @return
     */
    @Override
    public String convertToJavaData(ReadConverterContext<?> context) {
        String val = null;
        try {
            HashMap<String, String> map = new HashMap<>();
            map.put("停售", "0");
            map.put("在售", "1");
            map.put("试卖", "2");
            map.put("部分停售", "3");
            map.put("清仓", "4");
            map.put("部分清仓", "5");

            if (context != null) {
                val = map.get(context.getReadCellData().getStringValue());
            }
        } catch (Exception e) {

            val = null;
        }
        return val;
    }

    /**
     * 写的时候
     *
     * @param context
     * @return
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) throws Exception {
        String val = null;
        try {
            HashMap<String, String> map = new HashMap<>();
            map.put("0", "停售");
            map.put("1", "在售");
            map.put("2", "试卖");
            map.put("3", "部分停售");
            map.put("4", "清仓");
            map.put("5", "部分清仓");
            if (context != null) {
                val = map.get(context.getValue());
            }
        } catch (Exception e) {
            val = null;
        }
        return new WriteCellData<>(val);
    }
}
